package com.example.pceb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.VO.RecruitVo;
import com.example.pceb.entity.Collect;
import com.example.pceb.entity.Communication;
import com.example.pceb.entity.Recruit;
import com.example.pceb.entity.ResumeSend;
import com.example.pceb.mapper.CollectMapper;
import com.example.pceb.mapper.RecruitMapper;
import com.example.pceb.result.Result;
import com.example.pceb.result.ResultCode;
import com.example.pceb.service.Impl.ResumeSendService;
import com.example.pceb.service.RecruitService;
import com.example.pceb.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//学生端获取招聘信息
@RestController
//@CrossOrigin(origins = "*")
@RequestMapping("/recruits")
public class RecruitController {
    @Autowired
    private ResumeService resumeService;
    @Qualifier("recruitMapper")
    @Autowired
    private RecruitMapper recruitMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ResumeSendService resumeSendService;
    @Autowired
    private RecruitService recruitService;

    //学生获取招聘信息
    @PostMapping()
    public Result<List<Recruit>> list(@RequestBody Map<String, Object> params) {
        QueryWrapper<Recruit> wrapper = new QueryWrapper<>();

        //只查已审核职位
        wrapper.eq("isPass", 1);

        // 地点模糊匹配
        String address = (String) params.get("address");
        if (address != null && !address.trim().isEmpty()) {
            wrapper.like("address", address);
        }

        // 职位类型模糊匹配
        String identity = (String) params.get("identity");
        if (identity != null && !identity.trim().isEmpty()) {
            wrapper.like("identity", identity);
        }

        // 工作经验模糊匹配
        String experience = (String) params.get("experience");
        if (experience != null && !experience.trim().isEmpty()) {
            wrapper.like("experience", experience);
        }

        // 学历要求模糊匹配
        String education = (String) params.get("education");
        if (education != null && !education.trim().isEmpty()) {
            wrapper.like("education", education);
        }

        // 薪资待遇模糊匹配
        String salary = (String) params.get("salary");
        if (salary != null && !salary.trim().isEmpty()) {
            wrapper.like("salary", salary);
        }

        //关键词搜索
        String keyword = (String) params.get("keyword");
        if (keyword != null && !keyword.trim().isEmpty()) {
            wrapper.and(w -> w
                    .like("identity", keyword)
                    .or().like("enterprise", keyword)
                    .or().like("details", keyword)
                    .or().like("address", keyword)
                    .or().like("education", keyword)
                    .or().like("experience", keyword)
                    .or().like("salary", keyword)
            );
        }

        List<Recruit> list = recruitService.list(wrapper);
        return Result.success(list);
    }

    //不带筛选条件，返回所有招聘信息
    // 获取所有招聘信息（用于统计分析）
    @GetMapping("/all")
    public Result<List<Recruit>> all() {
        List<Recruit> list = recruitService.list();
        return Result.success(list);
    }

    //学生根据关键词搜索招聘职位
    @GetMapping("key")
    public Result<List<Recruit>> recruitsKey(String keyWords) {
        Result<List<Recruit>> listResult = resumeService.selectRecruitByKeyWords(keyWords);
        if (!listResult.getData().isEmpty()){
            return listResult;
        }else return Result.fail("未查询到符合关键词的职位", ResultCode.OK);
    }

    //学生发送简历
    @GetMapping("send")
    public Result<String> recruitsSend(String username,int recruitId) {
        return resumeService.sendRecruit(username,recruitId);
    }

    //学生获取招聘详细信息
    @GetMapping("detail")
    public Result<Map<String,Object>> getRecruitDetail(
            @RequestParam Integer recruitId,
            @RequestParam String username){
        return resumeService.getRecruitDetail(recruitId,username);
    }

    //收藏/取消收藏招聘
    @PostMapping("collect")
    public  Result<String> toggleCollect(
            @RequestParam Integer recruitId,
            @RequestParam String username
    ){
        return resumeService.toggleCollect(username,recruitId);
    }

    //点赞接口
    @PostMapping("like")
    public Result<String> likeRecruit(
            @RequestParam String username,
            @RequestParam Integer recruitId
    ){
        return resumeService.likeRecruit(username,recruitId);
    }

    //与HR聊天
    @PostMapping("communication")
    public Result<String> communicateWithHR(
            @RequestParam String username,
            @RequestParam Integer recruitId,
            @RequestParam String message
    ){
        return resumeService.communicateWithHR(username,recruitId,message);
    }

    //获取用户沟通历史
    @GetMapping("communication-history")
    public Result<List<Communication>> getCommunicationHistory(
            @RequestParam String username,
            @RequestParam Integer recruitId
    ){
        return resumeService.getCommunicationHistory(username,recruitId);
    }

    //获取热门招聘
    @GetMapping("hot")
    public Result<List<Recruit>> getHotRecruits(){
        LambdaQueryWrapper<Recruit> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Recruit::getIsHot,1)
                .orderByDesc(Recruit::getId);
        return Result.success(recruitMapper.selectList(wrapper));
    }

    //获取用户收藏列表
    @GetMapping("collections")
    public Result<List<Recruit>> getUserCollections(
            @RequestParam String username
    ){
        //获取收藏的招聘ID列表
        List<Integer> recruitIds =collectMapper.selectList(
                        new LambdaQueryWrapper<Collect>()
                                .select(Collect::getRecruitId)
                                .eq(Collect::getUsername, username))
                .stream()
                .map(Collect::getRecruitId)
                .collect(Collectors.toList());

        if (recruitIds.isEmpty()) {
            return Result.success(Collections.emptyList());
        }

        // 批量查询招聘信息
        return Result.success(recruitMapper.selectBatchIds(recruitIds));
    }

//    //检查收藏状态
//    @GetMapping("status")
//    public Result<Boolean> checkCollectStatus(
//            @RequestParam String username,
//            @RequestParam int recruitId) {
//        boolean isCollected = resumeService.isCollected(username, recruitId);
//        return Result.success("查询成功",isCollected);
//    }

    //查看相似职位信息
    @PostMapping("similar")
    public Result<List<Recruit>> getSimilarRecruits(@RequestBody Map<String, Object> params) {
        String enterprise = (String) params.get("enterprise");
        Integer currentId = params.get("currentId") != null ? Integer.parseInt(params.get("currentId").toString()) : null;
        List<Recruit> list = resumeService.getSimilarRecruits(enterprise, currentId);
        return Result.success(list);
    }
}
